export function timeFix () {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome () {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent () {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader (callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () {}
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE () {
  const bw = window.navigator.userAgent
  const compare = (s) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate (id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}

// 简单的深度复制
export function cloneObject (obj) {
  const objString = JSON.stringify(obj)
  return JSON.parse(objString)
}

// 替换菜单名称
export function replaceEntityAlias (name, newVal, oldVal) {
  if (name.includes(oldVal)) {
    name = newVal + name.split(oldVal)[1]
  }
  return name
}

// 下拉组件滚动分离问题
export function getPopupContainer (triggerNode) {
  return triggerNode.parentNode
}

// 处理树形数据结构 ———— 去除没有孩子的 children
export function handleTreeData (nodes) {
  return nodes.map(item => {
    const hasChild = item.children && item.children.length > 0
    if (!hasChild) {
      delete item.children
      return item
    }
    return {
      ...item,
      children: handleTreeData(item.children)
    }
  })
}

export function findAllParent (node, tree, parentNodes = [], index = 0) {
  if (!node || node.fid === 0) {
    return
  }
  findParent(node, parentNodes, tree)
  const parentNode = parentNodes[index]
  findAllParent(parentNode, tree, parentNodes, ++index)
  return parentNodes
}

function findParent (node, parentNodes, tree) {
  for (let i = 0; i < tree.length; i++) {
    const item = tree[i]
    if (item.id === node.parentId) {
      parentNodes.push(item)
      return
    }
    if (item.children && item.children.length > 0) {
      findParent(node, parentNodes, item.children)
    }
  }
}

export const getBase64 = (file) => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.readAsDataURL(file)
    reader.onload = () => resolve(reader.result)
    reader.onerror = error => reject(error)
  })
}
/**
 * 格式化秒
 * @param result
 * @returns {string}
 */
 export function formatSecond (result) {
  const h = Math.floor((result / 3600) % 24)
  const m = Math.floor((result / 60) % 60)
  const s = Math.floor(result % 60)
  result = s + '秒'
  if (m > 0) {
    result = m + '分钟' + result
  }
  if (h > 0) {
    result = h + '小时' + result
  }

  return result
}
